﻿function Get-TSMActivityLog {    
    <#
        .SYNOPSIS
            Get TSM activity log entries
                    
        .DESCRIPTION
            The Get-TSMActivityLog returns replication jobs from the TSM server

        .PARAMETER Search
            The string to search
            
        .PARAMETER AfterDate
            The start date for the search
            
        .PARAMETER BeforeDate
            The end date for the search

        .EXAMPLE
            Get-TSMActivityLog -Search "error"

        .NOTES
            Contributors:  David Seibel
            Last Revision Date: 09/02/2015

    #>
    [CmdletBinding()]
    Param(
        [Parameter(Position = 0)]
        $Search,
        
        [Parameter(Position = 1)]
        [DateTime]$AfterDate = (Get-Date).AddHours(-1),

        [Parameter(Position = 2)]
        [DateTime]$BeforeDate = (Get-Date),
        
        [Parameter(Position = 3)]
        [Int32]$MessageNumber,
        
        [Parameter(Position = 4)]
        $Node,
        
        [Parameter(Position = 5)]
        [ValidateSet("Server","Client")]
        $Originator,
        
        [Parameter(Position = 6)]
        $Owner,
        
        [Parameter(Position = 7)]
        $Schedule,
        
        [Parameter(Position = 8)]
        $Domain,
        
        [Parameter(Position = 9)]
        $SessionNumber,
        
        [Parameter(Position = 10)]
        [ValidateSet("Informational","Warning","Error")]
        $Severity
    )
    
    switch ($Severity) {
        "Informational" { $newSeverity = "I" }
        "Warning"       { $newSeverity = "W" }
        "Error"         { $newSeverity = "E" }
    }

    if ($AfterDate -lt $BeforeDate) {
        $whereProps = @()
        $whereProps += [PSCustomObject]@{ "Column" = "MESSAGE";    "Operator" = "LIKE"; "Value" = "*$Search*" }
        $whereProps += [PSCustomObject]@{ "Column" = "DATE_TIME";  "Operator" = ">=";   "Value" = $AfterDate }
        $whereProps += [PSCustomObject]@{ "Column" = "DATE_TIME";  "Operator" = "<=";   "Value" = $BeforeDate }
        $whereProps += [PSCustomObject]@{ "Column" = "MSGNO";      "Operator" = "=";    "Value" = $MessageNumber }
        $whereProps += [PSCustomObject]@{ "Column" = "NODENAME";   "Operator" = "LIKE"; "Value" = $Node }
        $whereProps += [PSCustomObject]@{ "Column" = "ORIGINATOR"; "Operator" = "=";    "Value" = $Originator }
        $whereProps += [PSCustomObject]@{ "Column" = "OWNERNAME";  "Operator" = "=";    "Value" = $Owner }
        $whereProps += [PSCustomObject]@{ "Column" = "SCHEDNAME";  "Operator" = "=";    "Value" = $Schedule }
        $whereProps += [PSCustomObject]@{ "Column" = "DOMAINNAME"; "Operator" = "=";    "Value" = $Domain }
        $whereProps += [PSCustomObject]@{ "Column" = "SESSION";    "Operator" = "=";    "Value" = $SessionNumber }
        $whereProps += [PSCustomObject]@{ "Column" = "SEVERITY";   "Operator" = "=";    "Value" = $newSeverity }

        $query = Format-TSMQuery -Table "ACTLOG" -Where $whereProps -OrderBy "DATE_TIME"
        $result = Invoke-TSMCommand -Command $query

        $defaultDisplaySet = 'DATE_TIME','MESSAGE'
        $typeDefinitions = @{
                             "DATE_TIME" = [DateTime]
                             "MSGNO"     = [Int32]
                             "PROCESS"   = [Int32]
                             "SESSID"    = [Int32]
                             "SESSION"   = [Int32]
                             "UNIQUE"    = [Int32]
                            }

        return Format-TSMData -Data $result -TypeDefinitions $typeDefinitions -DefaultDisplaySet $defaultDisplaySet
    } else {
        Write-Warning "Start date must be before the end date!"
    }
}